<?php




/* 清除购物车中过期的数据 */
function clear_cart(){
	$CommonModel	=	new CommonModel();
    /* 取得有效的session */
    $sql 		= 	"SELECT DISTINCT session_id " .
            		"FROM " . 'ybr_cart' . " AS c, " .
                	'ybr_sessions' . " AS s " .
            		"WHERE c.session_id = s.sesskey ";
    $valid_sess = 	$CommonModel->getCol($sql);

    // 删除cart中无效的数据
    $sql = "DELETE FROM " . 'ybr_cart' .
            " WHERE session_id NOT " . db_create_in($valid_sess);
    $CommonModel->query($sql);
}


/**
 * 获得用户的真实IP地址
 *
 * @access  public
 * @return  string
 */
function real_ip(){
    static $realip = NULL;
    if ($realip !== NULL){
        return $realip;
    }
    if (isset($_SERVER)){
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);

            /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
            foreach ($arr AS $ip){
                $ip = trim($ip);
                if ($ip != 'unknown'){
                    $realip = $ip;
                    break;
                }
            }
        }
        elseif (isset($_SERVER['HTTP_CLIENT_IP'])){
            $realip = $_SERVER['HTTP_CLIENT_IP'];
        }
        else{
            if (isset($_SERVER['REMOTE_ADDR'])){
                $realip = $_SERVER['REMOTE_ADDR'];
            }
            else{
                $realip = '0.0.0.0';
            }
        }
    }
    else{
        if (getenv('HTTP_X_FORWARDED_FOR')){
            $realip = getenv('HTTP_X_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_CLIENT_IP')){
            $realip = getenv('HTTP_CLIENT_IP');
        }
        else{
            $realip = getenv('REMOTE_ADDR');
        }
    }

    preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
    $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';

    return $realip;
}

/**
 * 递归方式的对变量中的特殊字符进行转义
 *
 * @access  public
 * @param   mix     $value
 *
 * @return  mix
 */
function addslashes_deep($value){
    if (empty($value)){
        return $value;
    }
    else{
        return is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
    }
}


/**
 * 清除缓存
 * Enter description here ...
 * 
 */
function clear_cache(){
	$dirname = './admin/Runtime/';
	@mkdir($dirname,0777,true); // 赋予该目录最大权限
  	rmdirr($dirname);
}	
function rmdirr($dirname) {
  	if (!file_exists($dirname)) {
  		return false;
  	}
  	if (is_file($dirname) || is_link($dirname)) {
   		return unlink($dirname);
  	}
  	$dir = dir($dirname);
  	if($dir){
   		while (false !== $entry = $dir->read()) {
			if ($entry == '.' || $entry == '..') {
	 			continue;
			}
			//递归
			rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
	   }
  	}
  	$dir->close();
 	return rmdir($dirname);
}

/**
 * 获得当前格林威治时间的时间戳
 *
 * @return  integer
 */
function gmtime(){
    //return (time() - date('Z'));
    return time();
}



/**
 * 创建像这样的查询: "IN('a','b')";
 *
 * @access   public
 * @param    mix      $item_list      列表数组或字符串
 * @param    string   $field_name     字段名称
 *
 */
function db_create_in($item_list, $field_name = ''){
    if (empty($item_list)){
        return $field_name . " IN ('') ";
    }
    else{
        if (!is_array($item_list)){
            $item_list 	= 	explode(',', $item_list);
        }
        $item_list 		= 	array_unique($item_list);
        $item_list_tmp 	= 	'';
        foreach ($item_list AS $item){
            if ($item !== ''){
                $item_list_tmp .= $item_list_tmp ? ",'$item'" : "'$item'";
            }
        }
        if (empty($item_list_tmp)){
            return $field_name . " IN ('') ";
        }
        else{
            return $field_name . ' IN (' . $item_list_tmp . ') ';
        }
    }
}

















